package com.ltu.config.shiro.dto;

import java.util.List;

import com.ltu.constant.CommonConstant;
import com.ltu.model.request.CommonRequest;
import com.ltu.util.common.IdsStrToListUtils;

import freemarker.template.utility.StringUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 用户对象
 */
@EqualsAndHashCode(callSuper=true)
public class UserDto extends CommonRequest {
	/*当小程序登录时username=ID password=token*/
	private String id;
	private String username;
	private List<String> roles;
	private List<String> userIds;	
	private Integer enable;
	private String jwtUserKey = "";
	/* 账号类型：1=前端登陆用户，2=后台登录,默认1 */
	private Integer loginType = 1;
	/* 前端类型：1=老师，2=学生,默认1 */
	private Integer frontType = 1;
	/* 权限等级：-1超管、 1全局、2部门管理、3普通 */
	private Integer authorityLevel = 3;
	private String password;
	
	public void setJwtUserKey(){
		StringBuilder tokenKey = new StringBuilder();
		tokenKey.append(this.id);
		tokenKey.append("_");	
		tokenKey.append(this.loginType);
		tokenKey.append("_");
		tokenKey.append(this.frontType == null ? "" : this.frontType);
		tokenKey.append("_");
		tokenKey.append(this.authorityLevel);
	
		if(roles != null && roles.size() > 0){
			tokenKey.append("_");
			tokenKey.append(IdsStrToListUtils.listToString(roles, ','));
		}
		this.jwtUserKey = tokenKey.toString();
	}
	public void setJwtUserKey(String jwtUserKey){
		this.jwtUserKey=jwtUserKey;
	}
	public String getJwtUserKey(){
		return this.jwtUserKey;
	}
	public static UserDto getUserDtoByStr(String tokenKey){
		String[]  obj = StringUtil.split(tokenKey, '_');
		UserDto u = new UserDto();
		u.setId(obj[0]);
		u.setLoginType(Integer.parseInt( obj[1]));
		u.setFrontType(Integer.parseInt( obj[2]));
		u.setAuthorityLevel(Integer.parseInt( obj[3]));
	
		if(obj.length > 4 && CommonConstant.LOGIN_TYPE_BACKSTAGE ==  u.getFrontType())
			u.setRoles(IdsStrToListUtils.getStringList(obj[4]));
		return u;
		
	}
	
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	
	public List<String> getRoles() {
		return roles;
	}
	public void setRoles(List<String> roles) {
		this.roles = roles;
	}
	public List<String> getUserIds() {
		return userIds;
	}
	public void setUserIds(List<String> userIds) {
		this.userIds = userIds;
	}
	public Integer getEnable() {
		return enable;
	}
	public void setEnable(Integer enable) {
		this.enable = enable;
	}
	public Integer getLoginType() {
		return loginType;
	}
	public void setLoginType(Integer loginType) {
		this.loginType = loginType;
	}
	public Integer getFrontType() {
		return frontType;
	}
	public void setFrontType(Integer frontType) {
		this.frontType = frontType;
	}
	public Integer getAuthorityLevel() {
		return authorityLevel;
	}
	public void setAuthorityLevel(Integer authorityLevel) {
		this.authorityLevel = authorityLevel;
	}

	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
	
}
